package com.zhanghu.backendZray.controller.permission;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhanghu.backendZray.annotation.MenuNeed;
import com.zhanghu.backendZray.manager.role.UserRoleMenuManager;
import com.zhanghu.backendZray.pojo.dto.ApiResponse;
import com.zhanghu.backendZray.pojo.request.OnlyIdRequest;
import com.zhanghu.backendZray.pojo.request.role.RolePageRequest;
import com.zhanghu.backendZray.pojo.request.role.RoleRequest;
import com.zhanghu.backendZray.pojo.vo.RoleVo;
import com.zhanghu.backendZray.service.RoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhangdong
 * @since 2024-07-20
 */
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
public class RoleController {
    private final RoleService rolesService;
    private final UserRoleMenuManager userRoleMenuManager;

    @MenuNeed(menuName = "role_manage")
    @PostMapping("add")
    public ApiResponse add(@Validated(RoleRequest.Create.class) @RequestBody RoleRequest roleRequest) {
        rolesService.add(roleRequest);
        return ApiResponse.ok();
    }

    @MenuNeed(menuName = "role_manage")
    @PostMapping("update")
    public ApiResponse update(@Validated(RoleRequest.Update.class) @RequestBody RoleRequest roleRequest) {
        rolesService.update(roleRequest);
        return ApiResponse.ok();
    }

    @MenuNeed(menuName = "role_manage")
    @PostMapping("delete")
    public ApiResponse delete(@RequestBody @NotNull OnlyIdRequest request) {
        Assert.notNull(request, "id不能为空");
        userRoleMenuManager.deleteRole(request.getId());
        return ApiResponse.ok();
    }

    /**
     * 分页查询
     *
     * @param
     * @return
     */
    @MenuNeed(menuName = "role_manage")
    @PostMapping("page")
    public ApiResponse<Page<RoleVo>> page(@RequestBody RolePageRequest rolePageRequest) {
        Page<RoleVo> pageInfo = rolesService.page(rolePageRequest);
        return ApiResponse.ok(pageInfo);
    }

}
